<template>
  <view class="body">
    <view class="name titleBold"> 您的血糖情况<span v-if="!isHaveDiabetes">（非必填）</span> </view>
    <view class="inputBox">
      <span class="name lineHeight86">空腹血糖</span>
      <input
        id="SBP"
        v-model="mainData.FBG"
        type="digit"
        class="uni-input lineHeight86 marginR120"
        name="FBG"
        placeholder-class="phcolor"
        @input="inputChange('FBG')"
      />
      <view class="unit">mmol/L</view>
      <view class="clear"></view>
    </view>
    <view class="inputBox">
      <span class="name lineHeight86">餐后2小时血糖</span>
      <input
        v-model="mainData.PBG"
        type="digit"
        class="uni-input lineHeight86 marginR120"
        name="PBG"
        placeholder-class="phcolor"
        @input="inputChange('PBG')"
      />
      <view class="unit">mmol/L</view>
      <view class="clear"></view>
    </view>
    <view class="inputBox">
      <span class="name lineHeight86">糖化血红蛋白</span>
      <input
        id="SBP"
        v-model="mainData.HBALC"
        type="digit"
        class="uni-input lineHeight86 marginR120"
        name="HBALC"
        placeholder-class="phcolor"
        @input="inputChange('HBALC')"
      />
      <view class="unit">%</view>
      <view class="clear"></view>
    </view>
  </view>
</template>

<script>
import { checkBetweenDecimalOne, decimalOne, checkBetween } from '@/common/assessKeyCheck';
// import { ajaxSend } from './ajaxHealth.js';
import { mapGetters } from 'vuex';
export default {
  data() {
    return {
      mainData: {
        FBG: '',
        PBG: '',
        HBALC: ''
      },
      index: 0
    };
  },
  computed: {
    ...mapGetters(['active', 'assessForm', 'isHaveDiabetes'])
  },
  mounted() {},
  methods: {
    validate() {
      // 血糖非必填
      if (this.isHaveDiabetes && !this.mainData.FBG) {
        uni.showToast({ title: `请输入空腹血糖`, icon: 'none' });
        return false;
      }
      if (this.isHaveDiabetes && !this.mainData.PBG) {
        uni.showToast({ title: `请输入餐后2小时血糖`, icon: 'none' });
        return false;
      }
      // ②录入值格式校验
      if (checkBetweenDecimalOne(this.mainData.FBG, '空腹血糖', 0.1, 15)) {
        uni.showToast({ title: `空腹血糖数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetweenDecimalOne(this.mainData.PBG, '餐后两小时血糖', 0.1, 15)) {
        uni.showToast({
          title: `餐后两小时血糖数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (decimalOne(this.mainData.HBALC)) {
        uni.showToast({
          title: `糖化血红蛋白数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (checkBetween(this.mainData.HBALC, '糖化血红蛋白', 3, 20)) {
        uni.showToast({
          title: `糖化血红蛋白数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      return this.mainData;
      // 提交vuex assessForm值
      // let submitData = this.assessForm;
      // // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      // const checkKeyIsInput = [
      //   {
      //     nameKey: 'FBG',
      //     title: '空腹血糖',
      //     healthGuidances: ['信息缺失，无法给出评估结论']
      //   },
      //   {
      //     nameKey: 'PBG',
      //     title: '餐后2小时血糖',
      //     healthGuidances: ['信息缺失，无法给出评估结论']
      //   },
      //   {
      //     nameKey: 'HBALC',
      //     title: '糖化血红蛋白',
      //     healthGuidances: ['信息缺失，无法给出评估结论']
      //   }
      // ];

      // let healthData = await ajaxSend(this.mainData);
      // Object.keys(this.mainData).forEach((key) => {
      //   submitData[key] = this.mainData[key];
      //   if (!this.mainData[key]) {
      //     healthData.push(...checkKeyIsInput.filter((item) => item.nameKey === key));
      //   }
      // });
      // let curActive = this.active + 1;
      // if (healthData && healthData.length > 0) {
      //   // 如果有异常，则提示
      //   this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
      //   this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      // } else {
      //   this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
      //   this.$store.commit('assessQuestion/SET_AJAXRES', false);
      // }
      // this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
    },

    inputChange(key) {
      this.$emit('choseList', key, this.mainData[key]);
    }
  }
};
</script>
